// pages/order/order.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
const pay = require('../../services/pay.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMore:true,
    loadingHidden:false,
    active: 0,
    page:1,
    size:10,
    orderList:[],
  },
  evaluate(e){
    let item = e.currentTarget.dataset.item
    wx.navigateBack({
      url: `/pages/success/success`
    })
  },
  links(e){
    let item = e.currentTarget.dataset.item
    if (item.pay_status !== 1){
      wx.navigateBack({
        url:`/pages/success/success?orderId=${item.id}`
      })
    }else{
      wx.navigateBack({
        url: `/pages/join/join?id=${item.orderGoodsVo.goods_id}&type=self`
      })
    }
  },

  onChange(event) {
    this.setData({
      active:event.detail.index,
      page:1,
      orderList: [],
      isMore:true,
      loadingHidden:false
    })
    this.getOrderList()
  },
  searchScrollLower(){
    this.getOrderList() 
  },

  getOrderList(){
    var that = this
    var order_status = this.data.active
    let data = { page: this.data.page, size: this.data.size, order_status: order_status}
    var orderListTem = this.data.orderList //定义一个数组使其等于data里定义的接收列表的数组
    util.request(api.orderList,data).then((res)=>{
      console.log(res)
      if(res.errno == 0){
        if(res.data.data.length != 0){
          res.data.data.forEach(v => {
            v.add_time = util.formatTime(new Date(v.add_time))//时间戳转时间
          })              
          if (this.data.page == 1){//当页数为1时，使上面定义的数组为空
            orderListTem = []
          }
          var orderList = res.data.data //再定义一个数组使其等于后台返回的数组
          //如果后台返回的数组长度小于我每页要取的数据的长度，说明已经是最后一页了
          if (orderList.length < this.data.size) { 
            this.setData({
              orderList: orderListTem.concat(orderList), //合并数组
              isMore: false,//是否还有更多数据：没有
              loadingHidden: true//隐藏加载框
            })
          } else {
            this.setData({
              orderList: orderListTem.concat(orderList),//合并数组
              isMore: true,//是否还有更多数据：有
              page: this.data.page + 1,//页数加1
              loadingHidden: true//隐藏加载框
            })
            
          }
        }
      }else{
        util.showErrorToast(res.msg)
      }
    })
  },

  pay(e){
    console.log(e)
    let orderId = e.currentTarget.id
    let order_type = e.currentTarget.dataset.order_type
    pay.payOrder(parseInt(orderId)).then(res => {
      if (order_type == 2) {
        wx.redirectTo({
          url: '/pages/success/success?orderId=' + orderId,
        })
      } else {
        wx.redirectTo({
          url: '/pages/order/order',
        })
      }
    }).catch(res => {
      util.alert(res.msg)
    });
  },

  cancelOrder(e){
    let item = e.currentTarget.dataset.item
    util.post(api.cancelOrder,{order_id:item.id}).then((res)=>{
      if (res.errno==0){
        util.showSuccessToast('取消成功')
        this.getOrderList()
      }else{
        util.showErrorToast(res.msg)
      }
    })
  },

  deleteOrder(e) {
    console.log(e)
    let item = e.currentTarget.dataset.item
    util.post(api.deleteOrder, { order_id: item.id }).then((res) => {
      if (res.errno == 0) {
        util.showSuccessToast('删除成功')
        this.setData({
          orderList:[],
          page:1
        })
        this.getOrderList()
      } else {
        util.showErrorToast(res.msg)
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.order_status !== undefined){
      this.setData({active:options.order_status})
      this.getOrderList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isMore){
      this.searchScrollLower()
    }else{
      util.showErrorToast('没有更多数据')
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var _this = this
    if (options.from == 'button') {
      console.log(options)
      let goodsInfo = options.target.dataset.item.orderGoodsVo
      return {
        title: goodsInfo.goods_name,
        imageUrl: goodsInfo.list_pic_url,
        path: `/pages/join/join?&inviteUserId=${wx.getStorageSync('userId')}&id=${goodsInfo.goods_id}&type=self`
      }

    }
  }
})